package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.result.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.DrawPrizeParam;
import com.example.lotterysystem.controller.param.ShowWinningRecordsParam;
import com.example.lotterysystem.controller.result.WinningRecordResult;
import com.example.lotterysystem.service.DrawPrizeService;
import com.example.lotterysystem.service.dto.WinningRecordDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/draw")
@RestController
public class DrawPrizeController {
    // 日志
    private static final Logger logger = LoggerFactory.getLogger(DrawPrizeController.class);

    @Autowired
    private DrawPrizeService drawPrizeService;

    /**
     * 使用异步处理的方式，也就是使用 RabbitMQ 的方式进行处理
     * 这里只需要返回成功即可，接⼝只做奖品数校验即可返回，这样前端就可以实现快速的响应
     * 进行 传参的参数 在后端进行自己校验，一定要成功，如果失败另想办法
     * @param param
     * @return
     */
    @RequestMapping("/prize")
    public CommonResult<Boolean> drawPrize(
            @Validated @RequestBody DrawPrizeParam param) {
        logger.info("drawPrize DrawPrizeParam:{}",
                JacksonUtil.writeValueAsString(param));
        drawPrizeService.drawPrize(param);

        return CommonResult.success(true);
    }

    /**
     * 获取获奖信息
     * 这里因为可能查询两种信息。
     * 1、当个的奖品查询 -> 只是查询一个奖品获取 ，传递 activityId、prizeId
     * 2、总体的中奖者获取  传递 activityId
     * @param param
     * @return
     */
    @RequestMapping("/show")
    public CommonResult<List<WinningRecordResult>> showWinningRecords(
            @Validated @RequestBody ShowWinningRecordsParam param) {
        logger.info("showWinningRecords ShowWinningRecordsParam:{}",
                JacksonUtil.writeValueAsString(param));
        List<WinningRecordDTO> winningRecordDTOList = drawPrizeService.getRecords(param);
        return CommonResult.success(
                convertToWinningRecordResultList(winningRecordDTOList));
    }

    private List<WinningRecordResult> convertToWinningRecordResultList(
            List<WinningRecordDTO> winningRecordDTOList) {
        if (CollectionUtils.isEmpty(winningRecordDTOList)) {
            return Arrays.asList();
        }

        return winningRecordDTOList.stream()
                .map(winningRecordDTO -> {
                    WinningRecordResult winningRecordResult = new WinningRecordResult();
                    winningRecordResult.setWinnerId(winningRecordDTO.getWinnerId());
                    winningRecordResult.setWinnerName(winningRecordDTO.getWinnerName());
                    winningRecordResult.setPrizeName(winningRecordDTO.getPrizeName());
                    winningRecordResult.setPrizeTier(winningRecordDTO.getPrizeTier().getMessage());
                    winningRecordResult.setWinningTime(winningRecordDTO.getWinningTime());
                    return winningRecordResult;
                }).collect(Collectors.toList());
    }
}
